package org.osami.androidui;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
 
public class OsamiUI extends FragmentActivity {
    /** Called when the activity is first created. */
	final static String TAG_LOG = "OsamiUI";
	
	protected static Activity instance;
	public static Activity getInstance(){
		return instance;
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  // ignore orientation/keyboard change
	  super.onConfigurationChanged(newConfig);
	}
		
	public boolean isTablet() { 		
		return true;
		/*
		Configuration conf = this.getBaseContext().getResources().getConfiguration();
		int size = conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
		// Comparing numeric to support pre 2.2 devices. No XLARGE there.
		Log.d(TAG_LOG, String.format("Got size ID:%d", size));
		return size >= Configuration.SCREENLAYOUT_SIZE_LARGE;
		*/	 
	} 
	
	/*
	private Vector<ContentFragment> listListeners = Vector<ContentFragment>;
	public void addListSelectionListener(ContentFragment fragment)
	{
		listListeners.add(fragment);
	}
	
	public void listItemSelected(OsamiServiceInfo service)
	{
		for (ContentFragment listener : this.listListeners) {
			
		}
	}*/
	
	public ContentFragment contentFragment = null;
	public void listItemSelected(OsamiServiceInfo service)
	{
		this.contentFragment.serviceChanged(service);
	}
	@Override
	protected void onDestroy() {
		OsamiUI.instance = null;
		super.onDestroy();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OsamiUI.instance = (Activity)this;
        
        if(this.isTablet()){
        	this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	        setContentView(R.layout.tablet_main);
        }
        else {
        	setContentView(R.layout.main);
        	
        	FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            
            ListFragment fragment = new ListFragment();
            fragmentTransaction.add(R.id.main, fragment);
            
            fragmentTransaction.commit();
        }
    }
}